<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en" xml:lang="en" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<head>
<META http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Guideline: Refactoring</title>
<meta name="uma.type" content="Guideline">
<meta name="uma.name" content="refactoring">
<meta name="uma.presentationName" content="Refactoring">
<meta name="element_type" content="other">
<meta name="filetype" content="description">
<meta name="role" content="">
<link rel="StyleSheet" href="./../../../css/default.css" type="text/css">
<script src="./../../../scripts/ContentPageResource.js" type="text/javascript" language="JavaScript"></script><script src="./../../../scripts/ContentPageSection.js" type="text/javascript" language="JavaScript"></script><script src="./../../../scripts/ContentPageSubSection.js" type="text/javascript" language="JavaScript"></script><script src="./../../../scripts/ContentPageToolbar.js" type="text/javascript" language="JavaScript"></script><script src="./../../../scripts/contentPage.js" type="text/javascript" language="JavaScript"></script><script type="text/javascript" language="JavaScript">
					var backPath = './../../../';
					var imgPath = './../../../images/';
					var nodeInfo=null;
					contentPage.preload(imgPath, backPath, nodeInfo,  '', false, false, false);
				</script>
</head>
<body>
<div id="breadcrumbs"></div>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td valign="top"><a name="Top"></a>
<div id="page-guid" value="_YNx2sJ05EdyQ3oTO93enUw"></div>
<table border="0" cellspacing="0" cellpadding="0" width="100%">
<tr>
<td class="pageTitle" nowrap="true">Guideline: Refactoring</td><td width="100%">
<div align="right" id="contentPageToolbar"></div>
</td><td width="100%" class="expandCollapseLink" align="right"><a name="mainIndex" href="./../../../index.htm"></a><script language="JavaScript" type="text/javascript" src="./../../../scripts/treebrowser.js"></script></td>
</tr>
</table>
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td class="pageTitleSeparator"><img src="./../../../images/shim.gif" alt="" title="" height="1"></td>
</tr>
</table>
<div class="overview">
<table width="97%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="50"><img src="./../../../images/guidance.gif" alt="" title=""></td><td>
<table class="overviewTable" border="0" cellspacing="0" cellpadding="0">
<tr>
<td valign="top">This guideline describes how to apply the refactoring technique to improve the quality of existing code.</td>
</tr>
</table>
</td>
</tr>
</table>
</div>
<div class="sectionHeading">Relationships</div>
<div class="sectionContent">
<table class="sectionTable" border="0" cellspacing="0" cellpadding="0">
<tr valign="top">
<th class="sectionTableHeading" scope="row">Related Elements</th><td class="sectionTableCell">
<ul>
<li>
<a href="./../../../practice.tech.evolutionary_design.base/guidances/guidelines/evolve_the_design_3C9D6965.html" guid="_C4U9QPTeEduDKIuqTXQ8SA">Evolve the Design</a>
</li>
<li>
<a href="./../../../practice.tech.evolutionary_design.base/guidances/roadmaps/how_to_adopt_evolutionary_design_7CA256E4.html" guid="_irQiEOCsEdynptYdmll41Q">How to Adopt the Evolutionary Design Practice</a>
</li>
<li>
<a href="./../../../practice.tech.test_driven_development.base/guidances/guidelines/test_driven_development_F581182D.html" guid="_5s_DUJ03EdyQ3oTO93enUw">Test Driven Development</a>
</li>
</ul>
</td>
</tr>
</table>
</div>
<div class="sectionHeading">Main Description</div>
<div class="sectionContent">
<table class="sectionTable" border="0" cellspacing="0" cellpadding="0">
<tr valign="top">
<td class="sectionTableSingleCell"><p>
    Refactoring involves improving the quality of existing code without changing the system's behavior. It is explicitly
    not about adding or changing behavior, but about improving the implementation quality of existing behavior.
</p>
<p>
    A full set of developer tests is required before refactoring can be safely applied. It is critical that the system
    behavior be in a known, verifiably correct state before modifying the implementation so that you can improve the
    quality without fear that the modified implementation will break something. Refactoring is a safe transformation to
    improve code, but it is safe only if there are tests that can verify that the system still works as intended.
</p>
<p>
    Refactoring is initiated when an area that needs improvement is identified in the system by examining either the code
    or&nbsp;some other&nbsp;representation of the design. The issues identified are sometimes called "smells."
</p>
<p>
    Here are several smells to look for that might lead to implementation or design refactoring:
</p>
<ul>
    <li>
        <b>Duplicated code:</b> Duplicated code makes the system harder to understand and harder to maintain.
    </li>
    <li>
        <b>Large design element or method:</b> Large design elements or methods diminish the ability of people to
        understand the code, reduce the potential for reuse, and make developer testing more difficult.
    </li>
    <li>
        <b>Poorly named element:</b> Whether the element be a variable, function, class, or implementation element, its
        name should connote what it is so that the code can be maintained.
    </li>
    <li>
        <strong>Tight coupling:</strong> Each design element should work with minimal concern for the internal aspects of
        other design elements. Otherwise, changes to one element can have undesirable effects in other elements.
    </li>
</ul>
<p>
    As you can see from this list, refactoring can improve the "internals" of an element or&nbsp;the interface of the
    element. Also, many of the smells&nbsp;are characterized as making the software more difficult to understand; whereas
    refactoring is about making the system simpler.
</p>
<p>
    After an issue is identified, a refactoring method can be selected that will improve the situation. There are catalogs
    of refactoring methods available that are change patterns that will fix common problems while retaining the behavior of
    the system.
</p>
<p>
    These are examples of refactoring methods:
</p>
<ul>
    <li>
        <b>Extract Method:</b> Pull out the duplicated code into its own single method or extract part of a large method
        into its own method.
    </li>
    <li>
        <b>Extract Class:</b> Pull some cohesive part of a class into its own class to reduce the size of a design element
        that is too big.
    </li>
    <li>
        <b>Rename Method</b>, <b>Rename Class</b>, or <b>Rename Variable:</b> Give a more meaningful name to an element to
        make it more understandable.
    </li>
    <li>
        <b>Extract Interface:</b> Create a clean interface.
    </li>
</ul>
<p>
    After refactoring has been applied, developer tests are run again to ensure that the system still behaves correctly. It
    is important that the system is working correctly after each small refactoring. Although many refactorings can be put
    together to drive broad change across the code base, the tests should run correctly between each refactoring applied.
    Refactoring must be applied as small behavior-preserving transformations.
</p>
<p>
    As mentioned previously, refactoring requires full developer test coverage of the area under consideration. There are
    additional techniques that enable refactoring. Coding standards define a common style and make it easier to refactor
    consistently. An attitude of&nbsp;collective code ownership within the team is important. Each developer should feel
    that refactoring can be applied across the code base to improve the implementation.
</p>
<p>
    For deeper coverage on this topic, including a listing of "smells" and a catalog of refactorings to respond to them,
    see [<a class="elementLinkWithUserText" href="./../../../core.default.nav_view.base/guidances/supportingmaterials/references_C6FF2A8D.html#FOW99" guid="__nHToFndEd2EdJKkAyeBng">FOW99</a>].
</p></td>
</tr>
</table>
</div>
<div class="sectionHeading">More Information</div>
<div class="sectionContent">
<table class="sectionTable" border="0" cellspacing="0" cellpadding="0">
<tr valign="top">
<th class="sectionTableHeading" scope="row">Checklists</th><td class="sectionTableCell">
<ul>
<li>
<a href="./../../../practice.tech.evolutionary_design.base/guidances/checklists/design_68980812.html" guid="_0XSzsMlgEdmt3adZL5Dmdw">Design</a>
</li>
<li>
<a href="./../../../core.tech.common.extend_supp/guidances/checklists/implementation_A5578577.html" guid="_etwusJ01EdyQ3oTO93enUw">Implementation</a>
</li>
</ul>
</td>
</tr>
<tr valign="top">
<th class="sectionTableHeading" scope="row">Concepts</th><td class="sectionTableCell">
<ul>
<li>
<a href="./../../../core.tech.common.extend_supp/guidances/concepts/coding_standard_1FF691E0.html" guid="_aGqAsJ01EdyQ3oTO93enUw">Coding Standard</a>
</li>
<li>
<a href="./../../../core.tech.common.extend_supp/guidances/concepts/refactoring_1B63BA3B.html" guid="_Poc7IPDzEdqYgerqi84oCA">Refactoring</a>
</li>
</ul>
</td>
</tr>
<tr valign="top">
<th class="sectionTableHeading" scope="row">Guidelines</th><td class="sectionTableCell">
<ul>
<li>
<a href="./../../../practice.tech.test_driven_development.base/guidances/guidelines/test_driven_development_F581182D.html" guid="_5s_DUJ03EdyQ3oTO93enUw">Test Driven Development</a>
</li>
</ul>
</td>
</tr>
</table>
</div>
<table class="copyright" border="0" cellspacing="0" cellpadding="0">
<tr>
<td class="copyright"><p> This program and the accompanying materials are made available under the<br />
  <a href="http://www.eclipse.org/org/documents/epl-v10.php" target="_blank">Eclipse 
  Public License V1.0</a>, which accompanies this distribution. </p><p/><p> <a class="elementLink" href="./../../../core.default.release_copyright.base/guidances/supportingmaterials/openup_copyright_C3031062.html" guid="_UaGfECcTEduSX6N2jUafGA">OpenUP Copyright</a></p></td>
</tr>
</table>
</td>
</tr>
</table>
</body>
<script type="text/javascript" language="JavaScript">
				contentPage.onload();
			</script>
</html>
